package bomberman.rule;

import gameframework.base.IntegerObservable;
import gameframework.base.Overlap;
import gameframework.game.GameUniverse;
import gameframework.game.OverlapRulesApplierDefaultImpl;

import java.util.Random;
import java.util.Vector;

import bomberman.BombermanGameLevel;
import bomberman.entity.Bomb;
import bomberman.entity.Bomberman;
import bomberman.entity.BonusCatchBomb;
import bomberman.entity.BonusIncreaseBombScope;
import bomberman.entity.BonusIncreaseNumberOfBombs;
import bomberman.entity.BonusRandom;
import bomberman.entity.BonusVomitoBomb;
import bomberman.entity.BreakableWall;
import bomberman.entity.Fire;



public class BombermanOverlapRules extends OverlapRulesApplierDefaultImpl {
	protected boolean managePacmanDeath;
	private IntegerObservable life[];

	public BombermanOverlapRules(IntegerObservable life[], int numberOfPlayer) {
		this.life = life;
	}

	public void setUniverse(GameUniverse universe) {
		//do nothing because of BombermanGameLevel.medUniverse.getUniverse()
	}

	@Override
	public void applyOverlapRules(Vector<Overlap> overlappables) {
		managePacmanDeath = true;
		super.applyOverlapRules(overlappables);
	}

	public void overlapRule(Bomberman b, Bomb bb){
		//the player can't move over a bomb.
		//if the bomb is the last the player dropped
		//he can move
		if (b.getBombPos().getX() == bb.getPosition().getX() &&
				b.getBombPos().getY() == bb.getPosition().getY())
			b.setOnBomb(true);
		else
			b.setOnBomb(false);
	}

	
	public void overlapRule(BreakableWall w, Fire f){
		w.breakWall();
	}
	
	public void overlapRule(Bomberman b, Fire f){
		if(b.isVulnerable())
		{
			life[b.getIdentity()].setValue(life[b.getIdentity()].getValue() - 1);
			if(life[b.getIdentity()].getValue() <= 0)
				BombermanGameLevel.medUniverse.getUniverse().removeGameEntity(b);
			else
				b.setVulnerable(false);
		}
	}
	
	public void overlapRule(Bomb b, Fire f){
		b.bombExplode();
	}
	
	public void overlapRule(Bomberman b, BonusIncreaseBombScope bonus){
		b.upBombPower();
		BombermanGameLevel.medUniverse.getUniverse().removeGameEntity(bonus);
	}
	
	public void overlapRule(Bomberman b, BonusIncreaseNumberOfBombs bonus){
		b.upNumberOfBombs();
		BombermanGameLevel.medUniverse.getUniverse().removeGameEntity(bonus);
	}
	
	public void overlapRule(Bomberman b, BonusCatchBomb bonus){
		b.setCanCatchBomb(true);
		BombermanGameLevel.medUniverse.getUniverse().removeGameEntity(bonus);
	}
	
	public void overlapRule(Bomberman b, BonusVomitoBomb bonus){
		b.vomitoBomb();
		BombermanGameLevel.medUniverse.getUniverse().removeGameEntity(bonus);
	}
	
	public void overlapRule(Bomberman b, BonusRandom bonus){
		Random val = new Random();
		switch (val.nextInt(4))
		{
		case 0:
			b.upBombPower();
			break;
		case 1:
			b.upNumberOfBombs();
			break;
		case 2:
			b.setCanCatchBomb(true);
			break;
		case 3:
			b.vomitoBomb();
			break;
		}
		BombermanGameLevel.medUniverse.getUniverse().removeGameEntity(bonus);
	}
	
	public void overlapRule(BonusIncreaseBombScope b, Fire f){
		BombermanGameLevel.medUniverse.getUniverse().removeGameEntity(b);
	}
	
	public void overlapRule(BonusIncreaseNumberOfBombs b, Fire f){
		BombermanGameLevel.medUniverse.getUniverse().removeGameEntity(b);
	}
	
	public void overlapRule(BonusRandom b, Fire f){
		BombermanGameLevel.medUniverse.getUniverse().removeGameEntity(b);
	}
	
	public void overlapRule(BonusCatchBomb b, Fire f){
		BombermanGameLevel.medUniverse.getUniverse().removeGameEntity(b);
	}
	
	public void overlapRule(BonusVomitoBomb b, Fire f){
		BombermanGameLevel.medUniverse.getUniverse().removeGameEntity(b);
	}

}
